// components/orderSheet/unpaidOrderItem.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //订单编号
    orderNo: {
      type: String,
      value: '20210715205309'
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: '/images',
    //剩余时间
    leftTime: '',
    //结束时间
    endTime: '2022-11-22',
    //订单项
    orderItems: [{
      itemId: '1',
      itemNum: 1,
      itemImgSrc: '/cart/item.jpg',
      itemName: 'MAGIC DRAGON魔杰龙轻柔倍适学步裤',
      itemDescrition: '尺寸：L',
      itemPrice: '138.00'
    }, {
      itemId: '2',
      itemNum: 1,
      itemImgSrc: '/cart/item.jpg',
      itemName: 'MAGIC DRAGON魔杰龙轻柔倍适学步裤',
      itemDescrition: '尺寸：L',
      itemPrice: '138.00'
    }],
    //价钱合计
    totalPrice: 0,
    //订单详情
    detail: null,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    goo_details(){
      console.log(6666);
    },
    /**
     * 整数小于10开头加0
     * @param {Number} d 数值
     */
    addZeroHead(d) {
      if (d < 10) return '0' + d;
      return d;
    },

    /**
     * 计算总价
     */
    calTotalPrice() {
      // let num = 0.0;
      // for (let item of this.data.orderItems) {
      //   num += Number(item.itemPrice);
      // }
      // console.log("num", num)

      this.setData({
        totalPrice: Number(this.data.detail.price).toFixed(2),
      })
    },
    /**
     * 取消订单
     * @param {*} e 
     */
    cancelOrder(e) {
      this.triggerEvent("cancelOrder", this.data.orderNo)

    },
    /**
     * 立即支付
     * @param {*} e 
     */
    payNow(e) {
      // console.log("triggerEvent:payNow,orderNo",this.data.orderNo);
      this.triggerEvent("payNow", this.data.orderNo)
    },
    /**
     * 计算剩余时间
     */
    calLeftTime() {
      setInterval(() => {
        // console.log("this.data.endTime",this.data.endTime)
        let endTime = new Date(this.data.endTime);
        let now = new Date();
        let dt = endTime - now;
        // console.log("dt", dt)
        if (dt < 0) dt = 0;

        //小时
        let hour = Math.floor(dt / (1000 * 60 * 60))
        // console.log("hour", hour)

        //分钟
        let dt1 = dt % (1000 * 60 * 60);
        let minute = Math.floor(dt1 / (1000 * 60));
        // console.log("minute", minute)

        //秒
        let dt2 = dt1 % (1000 * 60)
        let second = Math.floor(dt2 / 1000);
        // console.log("second", second)

        this.setData({
          leftTime: '待付款(' + this.addZeroHead(hour) + ':' + this.addZeroHead(minute) + ':' + this.addZeroHead(second) + ')'
        })
      }, 1000)
    },
    /**
     * 处理详情
     * @param {*} deta
     */
    handleDetail(data){
      const that=this;

      //订单项
      let orderItems = [];
      for (const item of data.goods_info) {
        orderItems.push({
          itemId: item.goods_id,
          itemNum: item.buy_number,
          itemImgSrc: item.image,
          itemName: item.goods_name,
          itemDescrition: item.stock_info.spec_value_name,
          itemPrice: item.price,
        })
      }

      //结束时间
      let created_at=data.created_at;
      let dtStart=new Date(created_at);
      let dtEnd=new Date(Number(dtStart)+600*1000);
      // console.log("dtEnd",dtEnd);

      that.setData({
        endTime: dtEnd,
        orderItems: orderItems,
      })

      // console.log("that.data",that.data)

      //设置一个定时器，计算付款剩余时间
      that.calLeftTime();

      //计算总价
      that.calTotalPrice()
    }
  },
  lifetimes: {
    attached() {
      const that = this;

      //图片路径
      const util = require("../../utils/util")
      this.setData({
        imgPath: util.getImgPath()
      })

      //从缓存取出orderList，如果有，直接使用
      let orderList = wx.getStorageSync('orderList')
      // console.log("orderList",orderList)
      if (!orderList) {

        //取订单详情
        const api = require("../../api/api")
        api.user.orderDetail({
          order_sn: that.data.orderNo,
          success(res) {
            // console.log("orderDetail,res", res)

            let data = res.data.data;

            //订单详情
            that.setData({
              detail: data,
            })

            //处理详情
            that.handleDetail(that.data.detail);

            /*
            //订单项
            let orderItems = [];
            for (const item of data.goods_info) {
              orderItems.push({
                itemId: item.goods_id,
                itemNum: item.buy_number,
                itemImgSrc: item.image,
                itemName: item.goods_name,
                itemDescrition: item.stock_info.spec_value_name,
                itemPrice: item.price,
              })
            }

            that.setData({
              endTime: data.complate_pay_time,
              orderItems: orderItems,
            })

            //设置一个定时器，计算付款剩余时间
            that.calLeftTime();

            //计算总价
            that.calTotalPrice()
            */
          }
        })
      }else{
        let data;
        for(let item of orderList){
          if(item.order_sn==that.data.orderNo){
            data=item;
            break;
          }
        }

        //订单详情
        that.setData({
          detail: data,
        })

        //处理详情
        that.handleDetail(data);
      }

    }
  }
})